package com.example.kliven.because.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.adapter.HomeShareLVAdapter;
import com.example.kliven.because.anim.CustomProgressDialog;
import com.example.kliven.because.bean.ShareBean;
import com.example.kliven.because.db.DaoSingleton;
import com.example.kliven.because.greendao.Share;
import com.example.kliven.because.greendao.ShareDao;
import com.example.kliven.because.intentbean.NetShareBean;
import com.example.kliven.because.utils.DayNightMode;
import com.google.gson.Gson;
import com.imooc.arcmenu.view.ArcMenu;

import java.util.ArrayList;
import java.util.List;

public class HomePageShareFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    private HomeShareLVAdapter homeShareLVAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<ShareBean> shareBeans;
    private ListView listView;
    private ArcMenu arcMenu;

    private RequestQueue queue;
    private TextView tv_most_topic;
    private Context context;
    private String url = "http://yinweiapi.paidui.com:81/appIndex/getIndexMessage.do?start=0&pageSize=15&memberId=0&cityId=0";

    // 数据库操作；
    private ShareDao shareDao = DaoSingleton.getInstance().getShareDao();
    private List<Share> shares = new ArrayList<Share>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_page_share_fragment, null);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.id_swipe_ly);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        listView = (ListView) view.findViewById(R.id.lv_home_share);
        arcMenu = (ArcMenu) view.findViewById(R.id.id_menu);

        getNetData();
        initEvent();
        return view;
    }

    private void initEvent() {
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (arcMenu.isOpen())
                    arcMenu.toggleMenu(500);
            }
        });

        arcMenu.setOnMenuItemClickListener(new ArcMenu.OnMenuItemClickListener() {
            @Override
            public void onClick(View view, int pos) {

                view.findViewById(R.id.btn_arcMenu_sleep).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int theme = DayNightMode.getDayNightMode(getActivity());
                        switch (v.getId()) {
                            case R.id.btn_arcMenu_sleep :
                                if (theme == DayNightMode.THEME_SUN) {
                                    DayNightMode.setDayNightMode(getActivity(),DayNightMode.THEME_NIGHT);
                                } else {
                                    DayNightMode.setDayNightMode(getActivity(),DayNightMode.THEME_SUN);
                                }
                                // 注意改过主题后一定要，把activity finish在重开一遍，因为更改主题只能在oncreat中进行
                                getActivity().finish();
                                getActivity().startActivity(new Intent(getActivity(),getActivity().getClass()));
                    }}
                });
            }
        });
    }

    public void getNetData() {
        queue = Volley.newRequestQueue(getActivity());
        final StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                shareBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetShareBean netShareBean = gson.fromJson(response, NetShareBean.class);

                for (int i = 0; i < netShareBean.getData().getApp_index_recommand().size(); i++) {
                    ShareBean shareBean = new ShareBean();
                    if (i < 2) {
                        shareBean.setShare_tag_name(netShareBean.getData().getApp_index_topic().get(i).getShare_tag_name());
                        shareBean.setShare_numb(netShareBean.getData().getApp_index_topic().get(i).getShare_num());
                        shareBean.setIco(netShareBean.getData().getApp_index_recommand().get(i).getIco());
                        shareBean.setMember_name(netShareBean.getData().getApp_index_recommand().get(i).getMember_name());
                        shareBean.setRecommand_time(netShareBean.getData().getApp_index_recommand().get(i).getRecommand_time());
                        shareBean.setTop_main_photo_url(netShareBean.getData().getIndex_index_latest().get(i).getMain_photo_url());
                        shareBean.setMain_photo_url(netShareBean.getData().getApp_index_recommand().get(i).getMain_photo_url());
                        shareBean.setShop_name(netShareBean.getData().getApp_index_recommand().get(i).getShop_name());
                        shareBean.setDish_name(netShareBean.getData().getApp_index_recommand().get(i).getDish_name());

                        Share share = new Share();
                        share.setShare_tag_name(netShareBean.getData().getApp_index_topic().get(i).getShare_tag_name());
                        share.setShare_numb(netShareBean.getData().getApp_index_topic().get(i).getShare_num());
                        share.setIco(netShareBean.getData().getApp_index_recommand().get(i).getIco());
                        share.setMember_name(netShareBean.getData().getApp_index_recommand().get(i).getMember_name());
                        share.setRecommand_time(netShareBean.getData().getApp_index_recommand().get(i).getRecommand_time());
                        share.setTop_main_photo_url(netShareBean.getData().getIndex_index_latest().get(i).getMain_photo_url());
                        share.setMain_photo_url(netShareBean.getData().getApp_index_recommand().get(i).getMain_photo_url());
                        share.setShop_name(netShareBean.getData().getApp_index_recommand().get(i).getShop_name());
                        share.setDish_name(netShareBean.getData().getApp_index_recommand().get(i).getDish_name());
                        shares.add(share);
                    } else {
                        shareBean.setIco(netShareBean.getData().getApp_index_recommand().get(i).getIco());
                        shareBean.setMember_name(netShareBean.getData().getApp_index_recommand().get(i).getMember_name());
                        shareBean.setRecommand_time(netShareBean.getData().getApp_index_recommand().get(i).getRecommand_time());
                        shareBean.setMain_photo_url(netShareBean.getData().getApp_index_recommand().get(i).getMain_photo_url());
                        shareBean.setShop_name(netShareBean.getData().getApp_index_recommand().get(i).getShop_name());
                        shareBean.setDish_name(netShareBean.getData().getApp_index_recommand().get(i).getDish_name());

                    }
                    shareBeans.add(shareBean);
//                    shareDao.insertInTx(shares);
                }
                homeShareLVAdapter = new HomeShareLVAdapter(getActivity(), shareBeans);
                listView.setAdapter(homeShareLVAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                shareDao.deleteInTx();
            }
        });
        queue.add(request);
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
                getNetData();
            }
        }, 5000);
        new CustomProgressDialog(getActivity(), "正在加载中", R.anim.frame_refesh).show();
    }
}